import { Col } from "../../../../core/data_ext";
import Bullet from "../../bullet";
import { GetEditorAttrListByTarget } from "../../bullet_editor_attribute";
import BulletAttrItem from "./bullet_attr_item";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BulletInfo extends cc.Component {
    @property(cc.Label)
    lbClassName: cc.Label = null;
    @property(cc.Node)
    attrContain: cc.Node = null;
    @property(cc.Prefab)
    attrItemPrefab: cc.Prefab = null;
    @property(cc.Prefab)
    attrItemVec2Prefab: cc.Prefab = null;
    @property(cc.Prefab)
    attrItemBooleanPrefab: cc.Prefab = null;
    @property(cc.Prefab)
    attrItemPrefabNumber: cc.Prefab = null;
    @property(cc.Prefab)
    attrItemColorPrefab: cc.Prefab = null;

    bullets: Bullet[] = [];
    attrItemCol: Col<BulletAttrItem> = {};
    setBullet(bullets: Bullet[]) {
        // if (this.bullets == bullets) {
        //     return;
        // }
        this.bullets = bullets;
        if (this.bullets.length > 0) {
            this.node.active = true;
            this.lbClassName.string = "";

            //记录赋值的次数，判断是否需要使用setValAdditive
            let setValTime: Col<number> = {};
            this.bullets.forEach(bul => {
                this.lbClassName.string += bul.name;

                let eac = GetEditorAttrListByTarget(bul);

                if (!eac) {
                    return;
                }
                for (let i = 0; i < eac.items.length; i++) {
                    let eaItem = eac.items[i];
                    let key = eaItem.property;

                    let val = bul[key];
                    let itemNode: cc.Node = null;
                    switch (eaItem.type) {
                        case "color":
                            itemNode = cc.instantiate(this.attrItemColorPrefab);
                            break;
                        case "vec2":
                            itemNode = cc.instantiate(this.attrItemVec2Prefab);
                            break;
                        case "boolean":
                            itemNode = cc.instantiate(this.attrItemBooleanPrefab);
                            break;
                        case "number":
                            itemNode = cc.instantiate(this.attrItemPrefabNumber);
                            break;
                        case "string":
                            itemNode = cc.instantiate(this.attrItemPrefab);
                            break;
                        default:
                            itemNode = cc.instantiate(this.attrItemPrefab);
                            break;
                    }
                    let item = this.attrItemCol[key];
                    if (!item) {
                        item = itemNode.getComponent(BulletAttrItem);
                        this.attrContain.addChild(item.node);
                        item.setVal(eaItem.name, val);
                        this.attrItemCol[key] = item;
                        setValTime[key] = 1;
                    }
                    else {
                        if (!setValTime[key]) {
                            setValTime[key] = 1;
                            item.setVal(eaItem.name, val);
                        }
                        else {
                            setValTime[key]++;
                            item.setValAdditive(eaItem.name, val);
                        }
                    }
                    item.on("edit", (newVal) => {
                        switch (eaItem.type) {
                            case "color":
                                bul[key] = newVal;
                                break;
                            case "vec2":
                                bul[key] = newVal;
                                break;
                            case "boolean":
                                bul[key] = newVal;
                                break;
                            case "number":
                                bul[key] = Number(newVal);
                                break;
                            case "string":
                                bul[key] = newVal;
                                break;
                            default:
                                bul[key] = newVal;
                                break;
                        }
                        bul.onMapEditorSetAttr(key);
                    }, this);
                }
            });
        }
        else {
            this.attrContain.destroyAllChildren();
            this.attrItemCol = {};
            this.node.active = false;
            this.lbClassName.string = "";
        }
    }
};